// JavaScript Document

		var arcid = $("#dows").attr("rel");
		var curPage = 1; //当前页码
        var total,pageSize,totalPage;
        //获取数据
        function getData(page){ 
            $.ajax({
                type: 'POST',
                url: '/i/dows-list-json.php?aid='+arcid,
                data: {'pageNum':page-1},
                dataType:'json',
                beforeSend:function(){
                    $("#list").append("<div id='loading'><img src='/images/loadinglit.gif' /><br>loading...</div>");
                },
                success:function(json){
                    $("#list").empty();
                    total = json.total; //总记录数
                    pageSize = json.pageSize; //每页显示条数
                    curPage = page; //当前页
                    totalPage = json.totalPage; //总页数
                    var li = "";
                    var list = json.list;
                    $.each(list,function(index,array){ //遍历json数据列
						li += "<div class='shoplist shopitem clearfix'>";
                        li += "<div class='uhead'><a href='/u/"+array['userid']+"/'><img src='"+array['face']+"' width='38' height='38'></a></div>";
						li += "<div class='shoptxt'>";

						li += "<div class='pcontent'>"+array['uname']+"【"+array['membername']+"】"+diaplayTime(array['dtime'])+"<font style='color:red;'>下载"+array['count']+"次</font></div>";
						li += "<div class='pcontent2'></div>";
						li += "</div>";
						li += "</div>";
                    });
                    $("#list").append(li);
                },
                complete:function(){ //生成分页条
                    getPageBar();
                },
                error:function(){
                    alert("数据加载失败");
                }
            });
        }
		
		
		function diaplayTime(data) {

            var str = data;
            //将字符串转换成时间格式
            var timePublish = new Date(str);
            var timeNow = new Date();
            var minute = 1000 * 60;
            var hour = minute * 60;
            var day = hour * 24;
            var month = day * 30;
            var diffValue = timeNow - timePublish;
            var diffMonth = diffValue / month;
            var diffWeek = diffValue / (7 * day);
            var diffDay = diffValue / day;
            var diffHour = diffValue / hour;
            var diffMinute = diffValue / minute;

            if (diffValue < 0) {
                //alert("错误时间");
            }
            else if (diffMonth > 3) {
                result = timePublish.getFullYear()+"-";
                result += timePublish.getMonth() + "-";
                result += timePublish.getDate();
               // alert(result);
            }
            else if (diffMonth > 1) {
                result = parseInt(diffMonth) + "月前";
            }
            else if (diffWeek > 1) {
                result = parseInt(diffWeek) + "周前";
            }
            else if (diffDay > 1) {
                result = parseInt(diffDay) + "天前";
            }
            else if (diffHour > 1) {
                result = parseInt(diffHour) + "小时前";
            }
            else if (diffMinute > 1) {
                result = parseInt(diffMinute) + "分钟前";
            }
            else {
                result = "刚刚发表";
            }
            return result;
        }   
		
        //获取分页条
        function getPageBar(){
            //页码大于最大页数
            if(curPage>totalPage) curPage=totalPage;
            //页码小于1
            if(curPage<1) curPage=1;
            pageStr = "<span>共"+total+"条</span><span>"+curPage+" / "+totalPage+"</span>";
            
            //如果是第一页
            if(curPage==1){
                //pageStr += "<span>首页</span><span>上一页</span>";
            }else{
                pageStr += "<span><a href='javascript:void(0)' rel='1'>首页</a></span><span><a href='javascript:void(0)' rel='"+(curPage-1)+"'>上一页</a></span>";
            }
            
            //如果是最后页
            if(curPage>=totalPage){
                //pageStr += "<span>下一页</span><span>尾页</span>";
            }else{
                pageStr += "<span><a href='javascript:void(0)' rel='"+(parseInt(curPage)+1)+"'>下一页</a></span><span><a href='javascript:void(0)' rel='"+totalPage+"'>尾页</a></span>";
            }
                
            $("#pagecount").html(pageStr);
        }
        $(function(){
            getData(1);
            $("#pagecount span a").live('click',function(){
                var rel = $(this).attr("rel");
                if(rel){
                    getData(rel);
                }
	});
});